<template>
  <!-- 菜单页面 -->
  <div >
    <el-card>
    <!-- 添加顶级菜单 -->
    <el-button @click="openSaveMenuTopDialogVisible()" type="success" size="small">添加顶级菜单</el-button>
    <!-- 全查数据 -->
    <div class="belowBox">
       <div style="margin-top: 20px;">
            <template>
                  <el-table
                  :data="menuData"
                  style="width: 100%;margin-bottom: 20px;"
                  row-key="id"
                  border
                  default-expand-all
                  :tree-props="{children: 'childrenList'}">
                  <el-table-column
                  fixed
                  prop="menuName"
                  label="名称">
                  </el-table-column>
                  <el-table-column
                  label="图标">
                  <template slot-scope="scope">
                      <i :class="scope.row.icon" style="margin-left:10px"></i>
                  </template>
                  </el-table-column>
                  <el-table-column
                  prop="path"
                  label="路径">
                  </el-table-column>
                  <el-table-column
                  prop="createTime"
                  label="创建用户时间"
                  sortable>
                  </el-table-column>
                  <el-table-column
                  prop="updateTime"
                  label="修改用户时间"
                  sortable>
                  </el-table-column>
                  <el-table-column
                  prop="back"
                  label="备注">
                  </el-table-column>
                  <el-table-column
                  fixed="right"
                  label="操作"
                  width="360"
                  >
                  <template slot-scope="scope">
                      <el-button @click="selectMenuBackEntityByOne(scope.row)" type="primary" size="small">查看</el-button>
                      <el-button @click="deleteRoleBackEntityById(scope.row)" type="danger" size="small">删除</el-button>
                      <el-button @click="openUpdateMenuDialogVisible(scope.row)" type="success" size="small">编辑</el-button>
                      <el-button @click="openSaveMenuDialogVisible(scope.row.id)" type="info" size="small"  v-if="scope.row.parentId == 0">添加子级菜单</el-button>
                  </template>
                  </el-table-column>
                </el-table>
            </template>
        </div>
    </div>
    <!-- 单查菜单详情 -->
    <el-dialog
        :title="title"
        :visible.sync="selectMenuBackDialogVisible"
        width="40%"
        >
        <el-form :model="tableDataForm" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="text-align: left;">
            <el-form-item label="名称">
                <el-input v-text="tableDataForm.menuName" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="图标">
                 <i :class="tableDataForm.icon" style="margin-left:10px"></i>
            </el-form-item>
            <el-form-item label="路径">
                <el-input v-text="tableDataForm.path" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="创建菜单时间">
                <el-input v-text="tableDataForm.createTime" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="修改菜单时间">
                <el-input v-text="tableDataForm.updateTime" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-text="tableDataForm.back" style="width: 200px;"></el-input>
            </el-form-item>
        </el-form>
    </el-dialog>
     <!-- 添加子级，修改弹窗 -->
    <el-dialog
        :title="title"
        :visible.sync="saveOrUpdateMenuDialogVisible"
        :before-close="closeExpertFormDialog"
        width="40%"
        >
        <!-- 添加弹窗内容 -->
        <el-form :model="tableDataForm" ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" style="text-align: left;">
            <el-form-item label="名称" prop="menuName">
                <el-input v-model="tableDataForm.menuName" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="路径" prop="path" >
                <el-input v-model="tableDataForm.path" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="back">
                <el-input v-model="tableDataForm.back" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="图标" prop="icon">
                <icon-picker v-model="tableDataForm.icon" style="width: 400px;"></icon-picker>
            </el-form-item>
            <el-form-item label="父级" prop="parentId" style="display:none">
                <el-input v-model="tableDataForm.parentId" style="width: 200px;"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('ruleForm')">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdateMenuBackEntity('ruleForm')">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 添加顶级菜单 -->
    <el-dialog
        :title="title"
        :visible.sync="saveOrUpdateMenuTopDialogVisible"
        :before-close="closeExpertFormDialog"
        width="40%"
        >
        <!-- 添加弹窗内容 -->
        <el-form :model="tableDataForm" ref="ruleForm" :rules="rulesTop" label-width="100px" class="demo-ruleForm" style="text-align: left;">
            <el-form-item label="名称" prop="menuName">
                <el-input v-model="tableDataForm.menuName" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="路径" prop="path" v-show="tableDataForm.parentId != 0">
                <el-input v-model="tableDataForm.path" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="back">
                <el-input v-model="tableDataForm.back" style="width: 200px;"></el-input>
            </el-form-item>
            <el-form-item label="图标" prop="icon">
                <icon-picker v-model="tableDataForm.icon" style="width: 400px;"></icon-picker>
            </el-form-item>
            <el-form-item label="父级" prop="parentId" style="display:none">
                <el-input v-model="tableDataForm.parentId" style="width: 200px;"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetTopForm('ruleForm')">取 消</el-button>
            <el-button type="primary" @click="saveOrUpdateMenuBackEntity('ruleForm')">确 定</el-button>
        </span>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 添加顶级菜单表单规则
      rulesTop: {
        menuName: [ // 菜单名称
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 0, max: 20, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        back: [ // 备注
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 0, max: 20, message: '备注不能为空', trigger: 'blur' }
        ]
      },
      //  添加子级，修改表单规则
      rules: {
        menuName: [ // 菜单名称
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 0, max: 20, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        path: [ // 路径
          { required: true, message: '请输入路径', trigger: 'blur' },
          { min: 0, max: 30, message: '路径不能为空', trigger: 'blur' }
        ],
        back: [ // 备注
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 0, max: 30, message: '备注不能为空', trigger: 'blur' }
        ]
      },
      // 添加顶级菜单弹窗
      saveOrUpdateMenuTopDialogVisible: false,
      // 添加子级菜单弹窗
      saveOrUpdateMenuDialogVisible: false,
      // 添加，修改弹窗时显示的名字
      title: '',
      // 单查数据集，添加数据集，修改数据集
      tableDataForm: {},
      // 单查菜单详情弹窗
      selectMenuBackDialogVisible: false,
      // 全查菜单数据集
      menuData: []
    }
  },
  mounted () {
    // 全查菜单表
    this.selectMenuBackEntity()
  },
  methods: {
    // 添加点击X关闭是清空from表单数据
    closeExpertFormDialog (done) {
      this.$refs.ruleForm.resetFields()
      done() // done 用于关闭 Dialog
    },
    // 添加顶级弹窗里的取消按钮 点击取消是清空from表单数据
    resetTopForm (formName) {
      this.$refs[formName].resetFields()
      this.saveOrUpdateMenuTopDialogVisible = false // 关闭添加顶级菜单弹窗
    },
    // 添加子级弹窗里的取消按钮 点击取消是清空from表单数据
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.saveOrUpdateMenuDialogVisible = false // 关闭添加子级菜单，修改菜单弹窗
    },
    // 删除菜单
    deleteRoleBackEntityById (data) {
      if (data.childrenList == null) { // 点击删除按钮 判断菜单下面是否有子级 有子级不能删除
        this.$confirm('此操作将删除该菜单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.delete('/eat-service/menuBack/deleteMenuBackEntityById',
            {
              params: {
                id: data.id
              }
            }).then(res => {
            if (res.status === 200) {
              this.myMessage('success', '删除成功')
              this.selectMenuBackEntity() // 全查菜单信息
              this.$router.go(0) // 强制刷新页面
            } else {
              this.myMessage('warning', '删除失败')
            }
          }).catch(res => {
            this.myMessage('erroe', '删除失败，请联系管理员')
          })
        }).catch(() => {
          this.myMessage('info', '已取消删除')
        })
      } else {
        this.myMessage('info', '菜单下面有子级不能删除')
      }
    },
    // 添加子级菜单，修改菜单
    saveOrUpdateMenuBackEntity (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const formData = new FormData()
          formData.set('id', this.tableDataForm.id)
          formData.set('menuName', this.tableDataForm.menuName)
          formData.set('icon', this.tableDataForm.icon)
          formData.set('path', this.tableDataForm.path)
          formData.set('parentId', this.tableDataForm.parentId)
          formData.set('back', this.tableDataForm.back)
          this.$axios.post('/eat-service/menuBack/saveOrUpdateMenuBackEntity', formData
          ).then(res => {
            if (res.status === 200) {
              this.myMessage('success', '恭喜你，操作成功！') // 成功提示
              this.saveOrUpdateMenuDialogVisible = false // 关闭添加子级菜单，修改菜单弹窗
              this.saveOrUpdateMenuTopDialogVisible = false // 关闭添加顶级菜单弹窗
              this.tableDataForm = {
              } // 清空tableDataForm表单
              this.selectMenuBackEntity() // 全查
            } else {
              this.myMessage('warning', res.data.obj) // 提示消息
            }
          }).catch(error => {
            console.log(error)
            this.myMessage('warning', '对不起，保存失败！')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 打开修改弹窗
    openUpdateMenuDialogVisible (data) {
      this.title = '修改菜单信息'
      this.saveOrUpdateMenuDialogVisible = true // 打开添加子级菜单，修改菜单弹窗
      this.tableDataForm = JSON.parse(JSON.stringify(data))
    },
    // 打开添加子级菜单弹窗
    openSaveMenuDialogVisible (data) {
      this.title = '添加子级菜单信息'
      this.saveOrUpdateMenuDialogVisible = true // 打开添加子级菜单，修改菜单弹窗
      this.tableDataForm = {
        id: 0,
        parentId: data,
        path: ''
      }
    },
    // 打开添加顶级菜单弹窗
    openSaveMenuTopDialogVisible () {
      this.title = '添加顶级菜单信息'
      this.saveOrUpdateMenuTopDialogVisible = true // 打开添加顶级菜单弹窗
      this.tableDataForm = {
        id: 0,
        parentId: 0,
        path: ''
      }
    },
    // 单查菜单详情
    selectMenuBackEntityByOne (data) {
      this.title = '菜单详情信息'
      this.selectMenuBackDialogVisible = true // 打开菜单详情
      this.tableDataForm = data
    },
    // 全查菜单信息
    selectMenuBackEntity () {
      this.$axios.get('/eat-service/menuBack/selectMenuBackEntity'
      ).then(res => {
        this.menuData = res.data.obj
      })
    },
    // 弹窗提示
    myMessage (type, message) {
      this.$message({
        type: type,
        message: message
      })
    }
  }
}
</script>

<style>
    .abvoeBox{
        width: 100%;
        height: 60px;
        text-align: left;
        margin-top: 5px;
        /* background-color: royalblue; */
        /* position: relative; */
    }
    .abvoeBox_input{
        width: 200px;
        height: 50px;
        margin-left: 10px;
        /* position: absolute; */
    }
    .belowBox{
        margin-top: 20px;
    }
</style>
